<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="资金使用者" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入资金使用者"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资金使用时间" prop="usefundsTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.usefundsTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择资金使用时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="资金支付方式" prop="usefundsType">
        <el-select v-model="queryParams.usefundsType" placeholder="请选择资金支付方式" clearable size="small">
          <el-option
            v-for="dict in usefundsTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="资金去向单号" prop="usefundsOrder">
        <el-input
          v-model="queryParams.usefundsOrder"
          placeholder="请输入资金去向单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="usefundsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="资金使用记录编号" align="center" prop="id" />
      <el-table-column label="资金使用者" align="center" prop="createBy" />
      <el-table-column label="资金使用时间" align="center" prop="usefundsTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.usefundsTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="本次使用资金金额" align="center" prop="usefundsNumber" />
      <el-table-column label="资金支付方式" align="center" prop="usefundsType" :formatter="usefundsTypeFormat" />
      <el-table-column label="资金去向单号" align="center" prop="usefundsOrder" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDownload(scope.row)"
          >下载</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看资金用途说明对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-descriptions column="1" title="资金用途详细信息">
          <el-descriptions-item label="资金使用时间">{{form.usefundsTime}}</el-descriptions-item>
          <el-descriptions-item label="本次使用金额">{{ form.usefundsNumber }}</el-descriptions-item>
          <el-descriptions-item label="资金支付方式">{{ usefundsTypeFormat(form)}}</el-descriptions-item>
          <el-descriptions-item label="资金去向单号">
            {{ form.usefundsOrder }}
          </el-descriptions-item>
          <el-descriptions-item label="资金用途说明">
            {{ form.usefundsMessage }}
          </el-descriptions-item>
          <el-descriptions-item label="备注">
            {{ form.remark }}
          </el-descriptions-item>
        </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  import { listUsefunds, getUsefunds, delUsefunds, addUsefunds, updateUsefunds } from "@/api/publish/usefunds/usefunds";
  import { getToken } from "@/utils/auth";

  export default {
    name: "Usefunds",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 资金用途说明表格数据
        usefundsList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 资金支付方式字典
        usefundsTypeOptions: [],
        // 上传参数
        upload: {
          // 是否禁用上传
          isUploading: false,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/common/upload",
          // 上传的文件列表
          fileList: []
        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          usefundsTime: null,
          usefundsNumber: null,
          usefundsType: null,
          usefundsOrder: null,
          usefundsMessage: null,
          usefundsFilename: null,
          usefundsFilepath: null,
          deptId: null,
          createBy: null,
          createById: null,
          updateById: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          usefundsTime: [
            { required: true, message: "资金使用时间不能为空", trigger: "blur" }
          ],
          usefundsNumber: [
            { required: true, message: "本次使用资金金额不能为空", trigger: "blur" },
            {
              pattern: /(^[1-9]([0-9]{0,10})?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]{1,2}$)/,
              message: '请输入正确使用金额,可保留两位小数'
            }
          ],
          usefundsType: [
            { required: true, message: "资金支付方式不能为空", trigger: "change" }
          ],
          usefundsOrder: [
            { required: true, message: "资金去向单号不能为空", trigger: "blur" }
          ],
          usefundsMessage: [
            { required: true, message: "资金用途说明不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("usefunds_type").then(response => {
        this.usefundsTypeOptions = response.data;
      });
    },
    methods: {
      /** 查询资金用途说明列表 */
      getList() {
        this.loading = true;
        listUsefunds(this.queryParams).then(response => {
          this.usefundsList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 资金支付方式字典翻译
      usefundsTypeFormat(row, column) {
        return this.selectDictLabel(this.usefundsTypeOptions, row.usefundsType);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          usefundsTime: null,
          usefundsNumber: null,
          usefundsType: null,
          usefundsOrder: null,
          usefundsMessage: null,
          usefundsFilename: null,
          usefundsFilepath: null,
          deptId: null,
          createBy: null,
          createById: null,
          createTime: null,
          updateBy: null,
          updateById: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.upload.fileList = [];
        this.title = "添加资金用途说明";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.upload.fileList = [{ name: this.form.fileName, url: this.form.filePath }];
        const id = row.id || this.ids
        getUsefunds(id).then(response => {
          this.form = response.data;
          this.open = true;
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {

              updateUsefunds(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();

              });
            } else {
              addUsefunds(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除资金用途说明编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delUsefunds(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      // 文件下载处理
      handleDownload(row) {
        var name = row.usefundsFilename;
        var url = row.usefundsFilepath;
        var suffix = url.substring(url.lastIndexOf("."), url.length);
        const a = document.createElement('a')
        a.setAttribute('download', name + suffix)
        a.setAttribute('target', '_blank')
        a.setAttribute('href', url)
        a.click()
      },
    }
  };
</script>
